package com.win.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * @Author:
 * @Date: 2024/7/19 16:20
 * @Description:
 */
public class CharacterEncodingFilter implements Filter {
    //初始化：web服务器启动时就已经初始化了，随时等待过滤对象出现
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    //Chain : 链
    /*
    1. 过滤中的所有代码，在过滤特定请求的时候都会执行
    2. 可能有多个过滤器，必须要让过滤器继续同行 filterChain.doFilter(request,response);
    */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html;charset=UTF-8");

        System.out.println("CharacterEncodingFilter执行前......");
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("CharacterEncodingFilter执行后......");


    }
    //销毁：web服务器关闭时，过滤才会销毁
    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
